package net.startingpoint;

import android.app.Activity;
import android.app.Dialog;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;

public class MainMenuActivity extends Activity {

	/*
	 * *********************************************************************
	 * Constants
	 * *********************************************************************
	 */

	public static final int IDENTIFIER = 0x0002;
	private static final int DIALOG_FIND_GAL = 0x001, DIALOG_FIND_FOOD = 0x002,
			DIALOG_RANDOM = 0x003;

	/*
	 * *********************************************************************
	 * Fields
	 * *********************************************************************
	 */

	private GpsNConnectionService mService;
	private AnimationDrawable animation;

	/*
	 * *********************************************************************
	 * Life cycle callback
	 * *********************************************************************
	 */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainmenu);

		Intent i = new Intent(MainMenuActivity.this,
				GpsNConnectionService.class);
		i.putExtra(GlobalConstants.IDENTIFIER_NAME, IDENTIFIER);
		bindService(i, mServiceConnection, Service.BIND_AUTO_CREATE);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		unbindService(mServiceConnection);
	}

	/*
	 * *********************************************************************
	 * User interaction
	 * *********************************************************************
	 */

	public void onStartMapActivity(View view) {
		Intent i = new Intent(MainMenuActivity.this, MappingActivity.class);
		startActivity(i);
	}

	public void onFindGirlClick(View view) {
		Toast.makeText(this, "Found 1 match", Toast.LENGTH_LONG).show();
		showDialog(DIALOG_FIND_GAL);
	}

	public void onFindFoodClick(View view) {
		showDialog(DIALOG_FIND_FOOD);
	}

	public void onRandomClick(View view) {
		Toast.makeText(MainMenuActivity.this, "Random Pressed",
				Toast.LENGTH_SHORT).show();
		showDialog(DIALOG_RANDOM);
	}

	@Override
	public void onBackPressed() {
		mService.disconnect();
		super.onBackPressed();
	}

	/*
	 * *********************************************************************
	 * Handler/Dispatcher (easy access to GUI thread)
	 * *********************************************************************
	 */

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case GlobalConstants.CONNECTION_LOST:
				finish();
				break;

			default:
				break;
			}
		}
	};

	/*
	 * *********************************************************************
	 * Service Connection
	 * *********************************************************************
	 */

	private ServiceConnection mServiceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName c) {
			mService = null;
		}

		@Override
		public void onServiceConnected(ComponentName c, IBinder b) {
			GpsNConnectionService.LocalBinder binder = (GpsNConnectionService.LocalBinder) b;
			mService = binder.bindMainMenuActivity(mHandler);
		}
	};

	private boolean isConnectedToService() {
		return mService != null;
	}

	/*
	 * *********************************************************************
	 * Sam's IU <3
	 * *********************************************************************
	 */

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_FIND_GAL:
			Dialog dialog = new Dialog(MainMenuActivity.this);
			dialog.setContentView(R.layout.girlofmydreams);
			dialog.setTitle("Google found 1 result");
			return dialog;
		case DIALOG_FIND_FOOD:
			Dialog toaster = new Dialog(MainMenuActivity.this);
			toaster.setContentView(R.layout.toastlayout);
			toaster.setTitle("This is a toast");
			
			ImageView image = (ImageView) toaster.findViewById(R.id.imageView1);
			image.setBackgroundResource(R.drawable.toaster);
			image.setScaleType(ScaleType.CENTER);
			animation = (AnimationDrawable) image.getBackground();
			image.post(new Runnable() {
				
				@Override
				public void run() {
					animation.start();
				}
			});
			return toaster;
		default:
			return super.onCreateDialog(id);
		}
	}

}
